/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csc411;

/**
 *
 * @author sgk
 */
public class test {

	public static void main(String args[]) throws Exception {

//		String str = "teststring";
//		byte[] bytes = Utils.stringToBytes(str);
//		System.out.println("str: ["+str+"]" );
//		System.out.println("bytes: ["+str.getBytes()+"] length:"+str.length()+":"+bytes );
//
//		for(int i=0; i<bytes.length; i++){
//			System.out.println("["+bytes[i]+"], ["+str.getBytes()[i]+"]");
//
//		}
		int num = -50;
		byte[] num_bytes = Utils.intToBytes(num);
		System.out.println("Num: " + num);
		System.out.print("Num in bytes: ");
		Utils.printBytes(num_bytes);
		System.out.println("Bytes back to int: " + Utils.bytesToInt(num_bytes));

		String str = "dfsfsdf";
		System.out.println("Str: " + str);
		byte[] str_bytes = Utils.fixedStringToBytes(str);
		System.out.print("Str in bytes: ");
		Utils.printBytes(str_bytes);
		System.out.println("Bytes back to string: " + Utils.bytesToFixedString(str_bytes));

		// purposely add new bytes to the str_bytes to check the the bytesToFixedString works properly
		byte[] str_bytes_long = new byte[5];
		System.arraycopy(str_bytes, 0, str_bytes_long, 0, 4);
		str_bytes_long[4] = 49;
		Utils.printBytes(str_bytes_long);
		System.out.println("Bytes back to string: " + Utils.bytesToFixedString(str_bytes_long));

		System.out.println("Checking out the string-bytes conversions");

		String str2 = "Hello World";
		System.out.println("String: " + str2);
		byte[] str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		str2 = "H";
		System.out.println("String: " + str2);
		str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		str2 = "";
		System.out.println("String: " + str2);
		str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		str2 = "Hel";
		System.out.println("String: " + str2);
		str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		str2 = "Hell";
		System.out.println("String: " + str2);
		str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		str2 = "Hello";
		System.out.println("String: " + str2);
		str2_bytes = Utils.stringToBytes(str2);
		System.out.println("Bytes back to string: " + Utils.bytesToString(str2_bytes));

		Currency type = Currency.USD;
		System.out.println(type.values()[1]);

		// testing out bit operations
		System.out.println();
		System.out.println("Testing out bit operations");
		byte[] messageInfo = new byte[4];

		// Clear all bits to 0;
		for (int i = 0; i < 4; i++) {
			messageInfo[i] = 0;
		}
		System.out.print("Clear bits to zero");
		Utils.printBytes(messageInfo);

		// set bit 0
		messageInfo[3] |= 0x0001;
		System.out.print("Set bit 0");
		Utils.printBytes(messageInfo);

		// set bit 2
		messageInfo[3] |= 0x0004;
		System.out.print("Set bit 2");
		Utils.printBytes(messageInfo);

		// set bit 1
		messageInfo[3] |= 0x0002;
		System.out.print("Set bit 1");
		Utils.printBytes(messageInfo);

		// clear bit 1
		messageInfo[3] &= 0xfffd;
		System.out.print("Clear bit 1");
		Utils.printBytes(messageInfo);

		// clear bit 2
		messageInfo[3] &= 0xfffb;
		System.out.print("Clear bit 2");
		Utils.printBytes(messageInfo);

		// clear bit 0
		messageInfo[3] &= 0xfffe;
		System.out.print("Clear bit 0");
		Utils.printBytes(messageInfo);

		// set bit 0
		Utils.setBit(messageInfo, 0, 1);
		System.out.print("Set bit 0");
		Utils.printBytes(messageInfo);

		// set bit 9
		Utils.setBit(messageInfo, 9, 1);
		System.out.print("Set bit 9");
		Utils.printBytes(messageInfo);

		// set bit 31
		Utils.setBit(messageInfo, 31, 1);
		System.out.print("Set bit 31");
		Utils.printBytes(messageInfo);

		int bit;
		// get bit 0
		bit = Utils.getBit(messageInfo, 0);
		System.out.println("Bit 0 = " + bit);
		// get bit 1
		bit = Utils.getBit(messageInfo, 1);
		System.out.println("Bit 1 = " + bit);
		// get bit 9
		bit = Utils.getBit(messageInfo, 9);
		System.out.println("Bit 9 = " + bit);
		// get bit 31
		bit = Utils.getBit(messageInfo, 31);
		System.out.println("Bit 31 = " + bit);

		// Nadiah (240210:0010)
		System.out.println();
		System.out.println("Checking out the float-bytes conversions");

		float flt1 = (float) Math.exp(1);
		System.out.println("Float: " + flt1);
		byte[] float1_bytes = Utils.floatToBytes(flt1);
		System.out.print("Float in bytes: ");
		Utils.printBytes(float1_bytes);
		System.out.println("Bytes back to Float: " + Utils.bytesToFloat(float1_bytes));

		// Nadiah (240210:0035)
		System.out.println();
		System.out.println("Checking out the currency-bytes conversions");

		byte[] curr_bytes1 = Utils.currencyToByte(Currency.SGD);
		System.out.print("Currency in bytes for SGD: ");
		Utils.printBytes(curr_bytes1);
		Currency c = Utils.byteToCurrency(curr_bytes1);
		System.out.println("Bytes back to currency: " + c);

		curr_bytes1 = Utils.currencyToByte(Currency.USD);
		System.out.print("Currency in bytes for USD: ");
		Utils.printBytes(curr_bytes1);
		c = Utils.byteToCurrency(curr_bytes1);
		System.out.println("Bytes back to currency: " + c);

		/////////////////////////////////
		// testing out AccountsManager //
		/////////////////////////////////
		System.out.println();
		System.out.println("Testing out Accounts Manager");
		System.out.println();
		AccountsManager am = new AccountsManager();
		Account temp;

		System.out.println("Get an existing account and show balance");
		try {
			Account sally = am.getAccount(1, "Sally Smith", "5678");
			System.out.println("Sally's Balance:" + sally.getBalance());


		} catch (Exception e) {
			System.out.println(e.toString());
		}

		System.out.println();
		System.out.println("Get a non-existing account");
		try {

			temp = am.getAccount(13, "Test", "TEST");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		System.out.println();
		System.out.println("Get an existing account with the wrong name");
		try {

			temp = am.getAccount(1, "Test", "TEST");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}


		System.out.println();
		System.out.println("Get an existing account with the wrong password");
		try {

			temp = am.getAccount(1, "Sally Smith", "TEST");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		System.out.println();
		System.out.println("Remove a non-existing account");
		try {
			am.removeAccount(13, "Test", "TEST");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		System.out.println();
		System.out.println("Remove an existing account with wrong name");
		try {
			am.removeAccount(2, "Margot Ng1", "TEST");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		System.out.println();
		System.out.println("Remove an existing account with wrong password");
		try {
			am.removeAccount(2, "Margot Ng", "TEST");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		System.out.println();
		System.out.println("Remove an existing account successfully");
		try {
			am.removeAccount(2, "Margot Ng", "9876");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		am.printAllAccounts();

		System.out.println();
		System.out.println("Try to get a deleted account ");
		try {
			am.removeAccount(2, "Margot Ng", "9876");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		/////////////////////////////////
		// testing out Messages		    //
		/////////////////////////////////
		System.out.println();
		System.out.println("Testing out Messages");
		System.out.println();

		Message m = new Message(1, 1, "hafiz", "1234", 123, Currency.SGD, 100f);

		/////////////////////////////////
		// testing out Rates		    //
		/////////////////////////////////
		System.out.println();
		System.out.println("Testing out Rates");

		float usd = 100f;
		float sgd = 100f;

		System.out.println("USD100 to SGD: SGD" + Rates.convert(usd, Currency.USD, Currency.SGD));
		System.out.println("SGD100 to USD: USD" + Rates.convert(sgd, Currency.SGD, Currency.USD));

	}
}
